﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Electric;
using YArchitech.MEP.Electric.Common;

namespace HYElectricSetting
{
    public partial class WireConduitForm : System.Windows.Forms.Form, IElectricSettingItem
	{
		public WireConduitForm(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			this.doc = doc;
			this.isEdited = false;
			this.isReady = false;
		}

		private void WireConduitForm_Load(object sender, EventArgs e)
		{
			this.InitButtonImage();
			this.PrepareSettingData();
		}

		private void WireConduitForm_Shown(object sender, EventArgs e)
		{
			this.isReady = true;
			this.UpdateFormData();
		}

		public bool IsEdited
		{
			get
			{
				return this.isEdited;
			}
			set
			{
				this.isEdited = value;
				HYElectricSettingMainForm hyelectricSettingMainForm = base.Tag as HYElectricSettingMainForm;
				bool flag = this.isEdited && hyelectricSettingMainForm != null;
				if (flag)
				{
					hyelectricSettingMainForm.ValidApplyButton();
				}
			}
		}

		public void UpdateFormData()
		{
			bool flag = this.isReady;
			if (flag)
			{
				this.FilterWireConduitSettingData();
				this.InitWireTypeCombox();
			}
		}

		public void SaveElectricSettingData()
		{
			ElectricSettingData.SaveWireConduitSettingData();
		}

		private void InitButtonImage()
		{
			this.buttonNewSize.Image = Resources.GetImage("Add20");
			this.buttonDelSize.Image = Resources.GetImage("Delete20");
			ToolTip toolTip = new ToolTip();
			toolTip.InitialDelay = 500;
			toolTip.AutoPopDelay = 3000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.buttonNewSize, "添加");
			toolTip.SetToolTip(this.buttonDelSize, "删除");
		}

		private void PrepareSettingData()
		{
			ElectricSettingData.InitWireSettingProcessData(this.doc);
			ElectricSettingData.InitConduitSettingProcessData(this.doc);
			ElectricSettingData.InitWireConduitSettingProcessData();
		}

		private void FilterWireConduitSettingData()
		{
			List<WireConduitData> list = ElectricSettingData.wireConduitSettingProcessData.ToList<WireConduitData>();
			ElectricSettingData.wireConduitSettingProcessData.Clear();
			using (List<WireConduitData>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					WireConduitData item = enumerator.Current;
					bool flag = !ElectricSettingData.WireSettingProcessData.Exists((WireSizeData wsd) => item.WireTypeName == wsd.WireType.Name);
					if (!flag)
					{
						WireSizeData wireSizeData = ElectricSettingData.WireSettingProcessData.Find((WireSizeData wsd) => item.WireTypeName == wsd.WireType.Name);
						bool flag2 = !ElectricSettingData.ConduitSettingProcessData.Exists((ConduitTypeData ctd) => item.ConduitTypeName == ctd.Name);
						if (!flag2)
						{
							ConduitTypeData conduitTypeData = ElectricSettingData.ConduitSettingProcessData.Find((ConduitTypeData ctd) => item.ConduitTypeName == ctd.Name);
							bool flag3 = wireSizeData == null || conduitTypeData == null;
							if (!flag3)
							{
								WireConduitData wireConduitData = new WireConduitData(item.WireTypeName, item.ConduitTypeName);
								foreach (KeyValuePair<double, int> keyValuePair in item.WireConduitSet)
								{
									bool flag4 = !wireSizeData.WireSizes.ContainsKey(keyValuePair.Key);
									if (!flag4)
									{
										bool flag5 = !conduitTypeData.SizeSet.Contains(keyValuePair.Value);
										if (!flag5)
										{
											wireConduitData.WireConduitSet.Add(keyValuePair.Key, keyValuePair.Value);
										}
									}
								}
								ElectricSettingData.wireConduitSettingProcessData.Add(wireConduitData);
							}
						}
					}
				}
			}
		}

		private void InitWireTypeCombox()
		{
			this.cmbWireType.Items.Clear();
			foreach (WireSizeData item in ElectricSettingData.WireSettingProcessData)
			{
				this.cmbWireType.Items.Add(item);
			}
			bool flag = this.cmbWireType.Items.Count > 0;
			if (flag)
			{
				this.cmbWireType.SelectedIndex = 0;
			}
		}

		private void UpdateConduitTypeCombox()
		{
			WireSizeData wireSizeData = (WireSizeData)this.cmbWireType.SelectedItem;
			bool flag = wireSizeData == null;
			if (!flag)
			{
				this.cmbConduitType.Items.Clear();
				foreach (WireConduitData wireConduitData in ElectricSettingData.wireConduitSettingProcessData)
				{
					bool flag2 = wireConduitData.WireTypeName != wireSizeData.WireType.Name;
					if (!flag2)
					{
						this.cmbConduitType.Items.Add(wireConduitData);
					}
				}
				bool flag3 = this.cmbConduitType.Items.Count > 0;
				if (flag3)
				{
					this.cmbConduitType.SelectedIndex = 0;
				}
				else
				{
					this.dataGridView.Rows.Clear();
				}
			}
		}

		private void UpdateDataGridView()
		{
			WireConduitData wireConduitData = (WireConduitData)this.cmbConduitType.SelectedItem;
			bool flag = wireConduitData == null;
			if (!flag)
			{
				this.dataGridView.Rows.Clear();
				foreach (KeyValuePair<double, int> keyValuePair in wireConduitData.WireConduitSet)
				{
					DataGridViewRow dataGridViewRow = new DataGridViewRow();
					int index = this.dataGridView.Rows.Add(dataGridViewRow);
					dataGridViewRow = this.dataGridView.Rows[index];
					dataGridViewRow.Cells[0].Value = keyValuePair.Key;
					dataGridViewRow.Cells[1].Value = keyValuePair.Value;
				}
			}
		}

		private void SelectDataGridViewRow(double key)
		{
			foreach (object obj in ((IEnumerable)this.dataGridView.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				double d = (double)dataGridViewRow.Cells[0].Value;
				bool flag = MathUtils.IsEqual(key, d, 0.1);
				if (flag)
				{
					dataGridViewRow.Selected = true;
					return;
				}
			}
			bool flag2 = this.dataGridView.Rows.Count > 0;
			if (flag2)
			{
				this.dataGridView.Rows[0].Selected = true;
			}
		}

		private void cmbWireType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateConduitTypeCombox();
		}

		private void cmbConduitType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateDataGridView();
		}

		private void buttonAdd_Click(object sender, EventArgs e)
		{
			WireSizeData wireSizeData = (WireSizeData)this.cmbWireType.SelectedItem;
			bool flag = wireSizeData == null;
			if (!flag)
			{
				List<string> list = new List<string>();
				foreach (object obj in this.cmbConduitType.Items)
				{
					WireConduitData wireConduitData = (WireConduitData)obj;
					list.Add(wireConduitData.ConduitTypeName);
				}
				List<string> list2 = new List<string>();
				foreach (ConduitTypeData conduitTypeData in ElectricSettingData.ConduitSettingProcessData)
				{
					list2.Add(conduitTypeData.Name);
				}
				bool flag2 = list2.Count <= 0;
				if (!flag2)
				{
					SelectSingleItemForm selectSingleItemForm = new SelectSingleItemForm("添加", list, list2);
					bool flag3 = selectSingleItemForm.ShowDialog() == DialogResult.OK;
					if (flag3)
					{
						WireConduitData wireConduitData2 = new WireConduitData(wireSizeData.WireType.Name, selectSingleItemForm.ResultItem);
						ElectricSettingData.wireConduitSettingProcessData.Add(wireConduitData2);
						this.UpdateConduitTypeCombox();
						this.cmbConduitType.SelectedItem = wireConduitData2;
						this.isEdited = true;
					}
				}
			}
		}

		private void buttonDel_Click(object sender, EventArgs e)
		{
			int selectedIndex = this.cmbConduitType.SelectedIndex;
			bool flag = selectedIndex < 0;
			if (!flag)
			{
				bool flag2 = MessageBox.Show("确定要删除选中项吗？", "盈建科提示", MessageBoxButtons.OKCancel) != DialogResult.OK;
				if (!flag2)
				{
					WireConduitData item = (WireConduitData)this.cmbConduitType.SelectedItem;
					this.cmbConduitType.Items.RemoveAt(selectedIndex);
					ElectricSettingData.wireConduitSettingProcessData.Remove(item);
					bool flag3 = selectedIndex < this.cmbConduitType.Items.Count;
					if (flag3)
					{
						this.cmbConduitType.SelectedIndex = selectedIndex;
					}
					this.IsEdited = true;
				}
			}
		}

		private void buttonNewSize_Click(object sender, EventArgs e)
		{
			WireSizeData wireSizeData = (WireSizeData)this.cmbWireType.SelectedItem;
			bool flag = wireSizeData == null;
			if (!flag)
			{
				WireConduitData wireConduitData = (WireConduitData)this.cmbConduitType.SelectedItem;
				bool flag2 = wireConduitData == null;
				if (flag2)
				{
					TaskDialog.Show("盈建科提示", "当前导线类型为空，请选择导线类型。");
				}
				else
				{
					ConduitTypeData conduitTypeData = ElectricSettingData.ConduitSettingProcessData.Find((ConduitTypeData ctd) => wireConduitData.ConduitTypeName == ctd.Name);
					bool flag3 = conduitTypeData == null;
					if (flag3)
					{
						TaskDialog.Show("盈建科提示", "当前线管类型为空，请选择线管类型。");
					}
					else
					{
						bool flag4 = wireSizeData.WireSizes.Count <= 0 || conduitTypeData.SizeSet.Count <= 0;
						if (flag4)
						{
							TaskDialog.Show("盈建科提示", "当前导线类型或线管类型的尺寸规格为空，不能添加导线穿管的对应关系。");
						}
						else
						{
							List<string> list = new List<string>();
							foreach (double num in wireSizeData.WireSizes.Keys)
							{
								list.Add(num.ToString());
							}
							List<string> list2 = new List<string>();
							foreach (int num2 in conduitTypeData.SizeSet)
							{
								list2.Add(num2.ToString());
							}
							List<string> list3 = new List<string>();
							foreach (double num3 in wireConduitData.WireConduitSet.Keys)
							{
								list3.Add(num3.ToString());
							}
							CreatePairItemForm createPairItemForm = new CreatePairItemForm("添加", list3, list, list2);
							bool flag5 = createPairItemForm.ShowDialog() == DialogResult.OK;
							if (flag5)
							{
								double key = Convert.ToDouble(createPairItemForm.WireResultItem);
								int value = Convert.ToInt32(createPairItemForm.ConduitResultItem);
								wireConduitData.WireConduitSet.Add(key, value);
								wireConduitData.WireConduitSorted();
								this.UpdateDataGridView();
								this.SelectDataGridViewRow(key);
								this.IsEdited = true;
							}
						}
					}
				}
			}
		}

		private void buttonDelSize_Click(object sender, EventArgs e)
		{
			WireConduitData wireConduitData = (WireConduitData)this.cmbConduitType.SelectedItem;
			bool flag = wireConduitData == null;
			if (!flag)
			{
				bool flag2 = this.dataGridView.SelectedRows.Count <= 0;
				if (!flag2)
				{
					DataGridViewRow dataGridViewRow = this.dataGridView.SelectedRows[0];
					bool flag3 = dataGridViewRow == null;
					if (!flag3)
					{
						int index = dataGridViewRow.Index;
						double key = (double)dataGridViewRow.Cells[0].Value;
						wireConduitData.WireConduitSet.Remove(key);
						this.UpdateDataGridView();
						bool flag4 = index < this.dataGridView.Rows.Count;
						if (flag4)
						{
							this.dataGridView.Rows[index].Selected = true;
						}
						this.IsEdited = true;
					}
				}
			}
		}

		private Document doc = null;

		private bool isEdited = false;

		private bool isReady = false;
	}
}
